package cn.tedu.csmall.order.webapi.kafka;

import cn.tedu.csmall.commons.pojo.order.entity.Order;
import com.google.gson.Gson;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class Producer {

    @Autowired
    private KafkaTemplate kafkaTemplate;
    public static final String TOPIC_KEY = "myOrder";
    int i = 1;
    @Scheduled(fixedRate = 10000)
    public void sendMessage(){
        Order order = new Order();
        order.setId(i++);
        order.setCommodityCode("PC100");
        order.setCount(1+ RandomUtils.nextInt(10));
        order.setMoney(10+ RandomUtils.nextInt(90));
        order.setUserId("UU100");

        Gson gson = new Gson();
        String json = gson.toJson(order);
        System.out.println("要发送的json字符串为:"+json);
        kafkaTemplate.send(TOPIC_KEY, json);
    }
}
